package com.aquarium.alarm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

import android.R.integer;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.telephony.SmsManager;
import android.util.Log;

import com.aquarium.R;
import com.aquarium.bt.BTManager;
import com.aquarium.bt.DataInterpreter;
import com.aquarium.bt.DataStruct;
import com.aquarium.config.AlarmDefaultValue;
import com.aquarium.config.BTCommand;
import com.aquarium.data.LocalDataManager;
import com.aquarium.ui.SensorValueDisplayActivity;
import com.aquarium.util.SPUtils;

//import com.aquarium.bt.RecvDataDispatcher;

public class AlarmManager extends Observable implements Observer {

	static String TAG = "AlarmManager";
	public static String INFO = "INFO";

	public final static String GET_PH_MIN_KEY = "GET_PH_MIN_KEY";
	public final static String GET_PH_MAX_KEY = "GET_PH_MAX_KEY";
	public final static String GET_PH_INTERVAL_KEY = "GET_PH_INTERVAL_KEY";
	public final static String GET_TEMPERATURE_MIN_KEY = "GET_TEMPERATURE_MIN_KEY";
	public final static String GET_TEMPERATURE_MAX_KEY = "GET_TEMPERATURE_MAX_KEY";
	public final static String GET_TEMPERATURE_INTERVAL_KEY = "GET_TEMPERATURE_INTERVAL_KEY";
	public final static String GET_OXYGEN_MIN_KEY = "GET_OXYGEN_MIN_KEY";
	public final static String GET_OXYGEN_MAX_KEY = "GET_OXYGEN_MAX_KEY";
	public final static String GET_OXYGEN_INTERVAL_KEY = "GET_OXYGEN_INTERVAL_KEY";
	public final static String GET_CONDUCTIVITY_MIN_KEY = "GET_CONDUCTIVITY_MIN_KEY";
	public final static String GET_CONDUCTIVITY_MAX_KEY = "GET_CONDUCTIVITY_MAX_KEY";
	public final static String GET_CONDUCTIVITY_INTERVAL_KEY = "GET_CONDUCTIVITY_INTERVAL_KEY";
	public final static String GET_WATER_LEVEL_MIN_KEY = "GET_WATER_LEVEL_MIN_KEY";
	public final static String GET_WATER_LEVEL_MAX_KEY = "GET_WATER_LEVEL_MAX_KEY";
	public final static String GET_WATER_LEVEL_INTERVAL_KEY = "GET_WATER_LEVEL_INTERVAL_KEY";
	public final static String GET_PHONE_NUM = "GET_PHONE_NUM";
	// public final static String GET_EMAIL_ADDR = "GET_EMAIL_ADDR";
	public final static String GET_PHONE_ENABLE = "GET_PHONE_ENABLE";
	public final static String GET_RANG_ENABLE = "GET_RANG_ENABLE";
	public final static String GET_ALARM_TIME = "GET_ALARM_TIME";

	public final static String ALARM_XML = "ALARM_XML";

	public final static int HANDLE_GET_PH_MIN = 0;
	public final static int HANDLE_GET_PH_MAX = 1;
	public final static int HANDLE_GET_TEMPERATURE_MIN = 2;
	public final static int HANDLE_GET_TEMPERATURE_MAX = 3;
	public final static int HANDLE_GET_OXYGEN_MIN = 4;
	public final static int HANDLE_GET_OXYGEN_MAX = 5;
	public final static int HANDLE_GET_CONDUCTIVITY_MIN = 6;
	public final static int HANDLE_GET_CONDUCTIVITY_MAX = 7;
	public final static int HANDLE_GET_WATER_LEVEL_MIN = 8;
	public final static int HANDLE_GET_WATER_LEVEL_MAX = 9;

	public final static int PH_ARALM_ACCURACY = 1;
	public final static int TEMPERATURE_ARALM_ACCURACY = 0;
	public final static int OXYGEN_ARALM_ACCURACY = 0;
	public final static int CONDUCTIVITY_ARALM_ACCURACY = 3;
	public final static int WATER_LEVEL_ARALM_ACCURACY = 0;

	public static final String MESSAGE_SENT_ACTION = "com.aquarium.alarm.MESSAGE_SENT";

	private float mPhMin;
	private float mPhMax;
	private float mTemperatureMin;
	private float mTemperatureMax;
	private float mOxygenMin;
	private float mOxygenMax;
	private float mConductivityMin;
	private float mConductivityMax;
	private float mWaterLevelMax;
	private float mWaterLevelMin;

	private String mPhoneNum;
	// private String mEmialAddr;
	private boolean mPhoneEnable;
	private boolean mRangEnable;

	// private float mIsWaterLevelNormal;

	private int mPhAlarmInterval;
	private int mTemperatureAlarmInterval;
	private int mOxygenAlarmInterval;
	private int mConductivityAlarmInterval;
	private int mWaterLevelAlarmInterval;

	private static SharedPreferences mSharedPreferences;
	private static SharedPreferences.Editor mEditor;
	private Context mContext;

	private static AlarmManager mAlarmManager;
	// private RecvDataDispatcher mRecvDataDispatcher;
	private static Uri rangUri;

	private BTManager mBtManager;
	private DataInterpreter mDataInterpreter;

	private Handler mAlarmManagerHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case HANDLE_GET_PH_MIN:
				mPhMin = (Float) msg.obj;
				notifyGetData(GET_PH_MIN_KEY, String.valueOf(mPhMin));
				break;
			case HANDLE_GET_PH_MAX:
				mPhMax = (Float) msg.obj;
				notifyGetData(GET_PH_MAX_KEY, String.valueOf(mPhMax));
				break;
			case HANDLE_GET_TEMPERATURE_MIN:
				mTemperatureMin = (Float) msg.obj;
				notifyGetData(GET_TEMPERATURE_MIN_KEY,
						String.valueOf(mTemperatureMin));
				break;
			case HANDLE_GET_TEMPERATURE_MAX:
				mTemperatureMax = (Float) msg.obj;
				notifyGetData(GET_TEMPERATURE_MAX_KEY,
						String.valueOf(mTemperatureMax));
				break;
			case HANDLE_GET_OXYGEN_MIN:
				mOxygenMin = (Float) msg.obj;
				notifyGetData(GET_OXYGEN_MIN_KEY, String.valueOf(mOxygenMin));
				break;
			case HANDLE_GET_OXYGEN_MAX:
				mOxygenMax = (Float) msg.obj;
				notifyGetData(GET_OXYGEN_MAX_KEY, String.valueOf(mOxygenMax));
				break;
			case HANDLE_GET_CONDUCTIVITY_MIN:
				mConductivityMin = (Float) msg.obj;
				notifyGetData(GET_CONDUCTIVITY_MIN_KEY,
						String.valueOf(mConductivityMin));
				break;
			case HANDLE_GET_CONDUCTIVITY_MAX:
				mConductivityMax = (Float) msg.obj;
				notifyGetData(GET_CONDUCTIVITY_MAX_KEY,
						String.valueOf(mConductivityMax));
				break;
			case HANDLE_GET_WATER_LEVEL_MIN:
				mWaterLevelMin = (Float) msg.obj;
				notifyGetData(GET_WATER_LEVEL_MIN_KEY,
						String.valueOf(mWaterLevelMin));
				break;
			case HANDLE_GET_WATER_LEVEL_MAX:
				mWaterLevelMax = (Float) msg.obj;
				notifyGetData(GET_WATER_LEVEL_MAX_KEY,
						String.valueOf(mWaterLevelMax));
				break;
			default:
				break;
			}

		};
	};

	private AlarmManager() {
		// mRecvDataDispatcher = RecvDataDispatcher.getInstance();
		// mRecvDataDispatcher.setAlarmManagerHandler(mAlarmManagerHandler);
		mBtManager = mBtManager.getInstance();
		mBtManager.addObserver(this);
		mDataInterpreter = new DataInterpreter();
	}

	private AlarmManager(Context context) {
		mContext = context;
		// mRecvDataDispatcher = RecvDataDispatcher.getInstance();
		// mRecvDataDispatcher.setAlarmManagerHandler(mAlarmManagerHandler);
		mSharedPreferences = mContext.getSharedPreferences(ALARM_XML,
				Context.MODE_PRIVATE);
		mEditor = mSharedPreferences.edit();
		intitAllValue();
	}

	public static AlarmManager getInstance(Context context) {
		if (mAlarmManager == null || mSharedPreferences == null) {
			mAlarmManager = new AlarmManager(context);
		}
		return mAlarmManager;
	}

	public static AlarmManager getInstance() {
		if (mAlarmManager == null) {
			mAlarmManager = new AlarmManager();
		}
		return mAlarmManager;
	}

	private void intitAllValue() {
		mConductivityAlarmInterval = SPUtils.getSP(mSharedPreferences,
				GET_CONDUCTIVITY_INTERVAL_KEY,
				AlarmDefaultValue.INTERVALConductivity);
		mConductivityMax = SPUtils.getSP(mSharedPreferences,
				GET_CONDUCTIVITY_MAX_KEY, AlarmDefaultValue.MAXConductivity);
		mConductivityMin = SPUtils.getSP(mSharedPreferences,
				GET_CONDUCTIVITY_MIN_KEY, AlarmDefaultValue.MINConductivity);
		mOxygenAlarmInterval = SPUtils.getSP(mSharedPreferences,
				GET_OXYGEN_INTERVAL_KEY, AlarmDefaultValue.INTERVALOxygen);
		mOxygenMax = SPUtils.getSP(mSharedPreferences, GET_OXYGEN_MAX_KEY,
				AlarmDefaultValue.MAXOxygen);
		mOxygenMin = SPUtils.getSP(mSharedPreferences, GET_OXYGEN_MIN_KEY,
				AlarmDefaultValue.MINOxygen);
		mPhAlarmInterval = SPUtils.getSP(mSharedPreferences,
				GET_PH_INTERVAL_KEY, AlarmDefaultValue.INTERVALPH);
		mPhMax = SPUtils.getSP(mSharedPreferences, GET_PH_MAX_KEY,
				AlarmDefaultValue.MAXPH);
		mPhMin = SPUtils.getSP(mSharedPreferences, GET_PH_MIN_KEY,
				AlarmDefaultValue.MINPH);
		mTemperatureAlarmInterval = SPUtils.getSP(mSharedPreferences,
				GET_TEMPERATURE_INTERVAL_KEY,
				AlarmDefaultValue.INTERVALTEMPERATURE);
		mTemperatureMax = SPUtils.getSP(mSharedPreferences,
				GET_TEMPERATURE_MAX_KEY, AlarmDefaultValue.MAXTEMPERATURE);
		mTemperatureMin = SPUtils.getSP(mSharedPreferences,
				GET_TEMPERATURE_MIN_KEY, AlarmDefaultValue.MINTEMPERATURE);
		mWaterLevelAlarmInterval = SPUtils.getSP(mSharedPreferences,
				GET_WATER_LEVEL_INTERVAL_KEY,
				AlarmDefaultValue.INTERVALWATERLEVEL);
		mWaterLevelMax = SPUtils.getSP(mSharedPreferences,
				GET_WATER_LEVEL_MAX_KEY, AlarmDefaultValue.MAXWATERLEVEL);
		mWaterLevelMin = SPUtils.getSP(mSharedPreferences,
				GET_WATER_LEVEL_MIN_KEY, AlarmDefaultValue.MINWATERLEVEL);
		mPhoneNum = SPUtils.getSP(mSharedPreferences, GET_PHONE_NUM,
				AlarmDefaultValue.PHONENUM);
		/*
		 * mEmialAddr = SPUtils.getSP(mSharedPreferences, GET_EMAIL_ADDR,
		 * AlarmDefaultValue.EMAILADDR);
		 */
		mPhoneEnable = SPUtils.getSP(mSharedPreferences, GET_PHONE_ENABLE,
				AlarmDefaultValue.ENABLEPHONE);

		mRangEnable = SPUtils.getSP(mSharedPreferences, GET_RANG_ENABLE,
				AlarmDefaultValue.ENABLEEMAIL);

		// Log.d("intitAllValue", "intitAllValue" + mEmialAddr + mPhoneNum);
	}

	public void setRangUri(Uri uri) {
		rangUri = uri;
	}

	public boolean isPhoneEnable() {
		return mPhoneEnable;
	}

	public void setPhoneEnable(boolean mPhoneEnable) {
		this.mPhoneEnable = mPhoneEnable;
		SPUtils.setSP(mEditor, GET_PHONE_ENABLE, mPhoneEnable);
	}

	public boolean isRangEnable() {
		return mRangEnable;
	}

	public void setRangEnable(boolean mRangEnable) {
		this.mRangEnable = mRangEnable;
		SPUtils.setSP(mEditor, GET_RANG_ENABLE, mRangEnable);
	}

	public String getPhoneNum() {
		return mPhoneNum;
	}

	public void setPhoneNum(String mPhoneNum) {
		if (mPhoneNum == null || mPhoneNum.equals(AlarmDefaultValue.PHONENUM)) {
			this.mPhoneNum = null;
		} else {
			this.mPhoneNum = mPhoneNum;
		}
		SPUtils.setSP(mEditor, GET_PHONE_NUM, mPhoneNum);
	}

	/*
	 * public String getEmialAddr() { return mEmialAddr; }
	 */

	/*
	 * public void setEmialAddr(String mEmialAddr) { if (mEmialAddr == null ||
	 * mEmialAddr.equals(AlarmDefaultValue.EMAILADDR)) { this.mEmialAddr = null;
	 * } else { this.mEmialAddr = mEmialAddr; } SPUtils.setSP(mEditor,
	 * GET_EMAIL_ADDR, mEmialAddr); }
	 */

	public float getPhMin() {
		return mPhMin;
	}

	public void setPhMin(float mPhMin) {
		this.mPhMin = mPhMin;
		SPUtils.setSP(mEditor, GET_PH_MIN_KEY, mPhMin);
		// TODO we need to composite a DataStruct and send to BTManage

	}

	public float getPhMax() {
		return mPhMax;
	}

	public void setPhMax(float mPhMax) {
		this.mPhMax = mPhMax;
		SPUtils.setSP(mEditor, GET_PH_MAX_KEY, mPhMax);
		// TODO we need to composite a DataStruct and send to BTManage

	}

	public float getTemperatureMin() {
		return mTemperatureMin;
	}

	public void setTemperatureMin(float mTemperatureMin) {
		this.mTemperatureMin = mTemperatureMin;
		SPUtils.setSP(mEditor, GET_TEMPERATURE_MIN_KEY, mTemperatureMin);
		// TODO we need to composite a DataStruct and send to BTManage

	}

	public float getTemperatureMax() {
		return mTemperatureMax;
	}

	public void setTemperatureMax(float mTemperatureMax) {
		this.mTemperatureMax = mTemperatureMax;
		SPUtils.setSP(mEditor, GET_TEMPERATURE_MAX_KEY, mTemperatureMax);
		// TODO we need to composite a DataStruct and send to BTManage

	}

	public float getOxygenMin() {
		return mOxygenMin;
	}

	public void setOxygenMin(float mOxygenMin) {
		this.mOxygenMin = mOxygenMin;
		SPUtils.setSP(mEditor, GET_OXYGEN_MIN_KEY, mOxygenMin);
		// TODO we need to composite a DataStruct and send to BTManage

	}

	public float getOxygenMax() {
		return mOxygenMax;
	}

	public void setOxygenMax(float mOxygenMax) {
		this.mOxygenMax = mOxygenMax;
		SPUtils.setSP(mEditor, GET_OXYGEN_MAX_KEY, mOxygenMax);
		// TODO we need to composite a DataStruct and send to BTManage

	}

	public float getConductivityMin() {
		return mConductivityMin;
	}

	public void setConductivityMin(float mConductivityMin) {
		this.mConductivityMin = mConductivityMin;
		SPUtils.setSP(mEditor, GET_CONDUCTIVITY_MIN_KEY, mConductivityMin);
		// TODO we need to composite a DataStruct and send to BTManage

	}

	public float getConductivityMax() {
		return mConductivityMax;
	}

	public void setConductivityMax(float mConductivityMax) {
		this.mConductivityMax = mConductivityMax;
		SPUtils.setSP(mEditor, GET_CONDUCTIVITY_MAX_KEY, mConductivityMax);
		// TODO we need to composite a DataStruct and send to BTManage

	}

	public float getWaterLevelMax() {
		return mWaterLevelMax;
	}

	public void setWaterLevelMax(float mWaterLevelMax) {
		this.mWaterLevelMax = mWaterLevelMax;
		SPUtils.setSP(mEditor, GET_WATER_LEVEL_MAX_KEY, mWaterLevelMax);
	}

	public float getWaterLevelMin() {
		return mWaterLevelMin;
	}

	public void setWaterLevelMin(float mWaterLevelMin) {
		this.mWaterLevelMin = mWaterLevelMin;
		SPUtils.setSP(mEditor, GET_WATER_LEVEL_MAX_KEY, mWaterLevelMin);
	}

	public int getPhAlarmInterval() {
		return mPhAlarmInterval;
	}

	public int getTemperatureAlarmInterval() {
		return mTemperatureAlarmInterval;
	}

	public int getOxygenAlarmInterval() {
		return mOxygenAlarmInterval;
	}

	public int getConductivityAlarmInterval() {
		return mConductivityAlarmInterval;
	}

	public int getWaterLevelInterval() {
		return mWaterLevelAlarmInterval;
	}

	public void setPhAlarmInterval(int second) {
		mPhAlarmInterval = second;
		SPUtils.setSP(mEditor, GET_PH_INTERVAL_KEY, mPhAlarmInterval);
		// TODO use SP to store data
	}

	public void setTemperatureAlarmInterval(int second) {
		mTemperatureAlarmInterval = second;
		SPUtils.setSP(mEditor, GET_TEMPERATURE_INTERVAL_KEY,
				mTemperatureAlarmInterval);

	}

	public void setOxygenAlarmInterval(int second) {
		mOxygenAlarmInterval = second;
		SPUtils.setSP(mEditor, GET_OXYGEN_INTERVAL_KEY, mOxygenAlarmInterval);

	}

	public void setConductivityAlarmInterval(int second) {
		mConductivityAlarmInterval = second;
		SPUtils.setSP(mEditor, GET_CONDUCTIVITY_INTERVAL_KEY,
				mConductivityAlarmInterval);

	}

	public void setWaterLevelAlarmInterval(int second) {
		mWaterLevelAlarmInterval = second;
		SPUtils.setSP(mEditor, GET_WATER_LEVEL_INTERVAL_KEY,
				mConductivityAlarmInterval);

	}

	public static void setAlarmTime() {
		long time = System.currentTimeMillis();
		SPUtils.setSP(mEditor, GET_ALARM_TIME, time);
	}

	// timestamp .when alarm rang ,send message,email
	public long getAlarmTime() {
		return SPUtils.getSP(mSharedPreferences, GET_ALARM_TIME, 0l);
	}

	/**
	 * Do alarm
	 */
	MediaPlayer alarmMusic;

	public void rang() {

		if (rangUri == null) {
			alarmMusic = MediaPlayer.create(mContext, RingtoneManager
					.getActualDefaultRingtoneUri(mContext,
							RingtoneManager.TYPE_RINGTONE));
		} else {
			alarmMusic = MediaPlayer.create(mContext, rangUri);
		}

		alarmMusic.setLooping(true);
		alarmMusic.setVolume(10, 10);
		// 播放音乐
		alarmMusic.start();
		TimerTask timerTask = new TimerTask() {
			@Override
			public void run() {
				if (alarmMusic.isPlaying() && alarmMusic != null) {
					alarmMusic.stop();
					alarmMusic.release();
					alarmMusic = null;
				}

			}
		};
		Timer timer = new Timer();
		timer.schedule(timerTask, 30 * 1000);

		alarmMusic.isPlaying();

	}

	public void closeRang() {
		if (alarmMusic.isPlaying() && alarmMusic != null) {
			alarmMusic.stop();
			alarmMusic.release();
			alarmMusic = null;
		}
	}

	/**
	 * Send SMS
	 */
	public void sendSms(String text) {
		IntentFilter intentFilter = new IntentFilter(MESSAGE_SENT_ACTION);
		mContext.registerReceiver(broadcastReceiver, intentFilter);

		String destinationAddress = getPhoneNum();
		if (destinationAddress == null || destinationAddress.equals(null)
				|| "null".equals(destinationAddress)
				|| "".equals(destinationAddress)) {
			Log.e(TAG, "destinationAddress is null");
			return;
		}
		Log.d(TAG, "destinationAddress:" + destinationAddress + " text:" + text);
		Intent intent = new Intent(MESSAGE_SENT_ACTION);
		PendingIntent sentIntent = PendingIntent.getBroadcast(mContext, 0,
				intent, 0);
		PendingIntent deliveryIntent = null;
		String scAddress = null;
		SmsManager smsManager = SmsManager.getDefault();
		ArrayList<String> texts = smsManager.divideMessage(text);
		for (String string : texts) {
			smsManager.sendTextMessage(destinationAddress, scAddress, string,
					sentIntent, deliveryIntent);
			Log.d("send message", string);
		}

		// TODO
	}

	private void registerReceiver(BroadcastReceiver broadcastReceiver2,
			IntentFilter intentFilter) {
		// TODO Auto-generated method stub

	}

	/**
	 * Send E-mail
	 */
	public void sendEmail() {
		// TODO
	}

	/**
	 * Send data to Observers, such as {@link SensorValueDisplayActivity} and
	 * {@link LocalDataManager}
	 * 
	 * @param dataKey
	 * @param dataValue
	 */
	private void notifyGetData(String dataKey, String dataValue) {
		setChanged();
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(dataKey, dataValue);
		notifyObservers(map);
	}

	public int getMinAlarmInterval() {
		int min;
		min = mPhAlarmInterval > mTemperatureAlarmInterval ? mTemperatureAlarmInterval
				: mPhAlarmInterval;
		min = min > mOxygenAlarmInterval ? mOxygenAlarmInterval : min;
		min = min > mConductivityAlarmInterval ? mConductivityAlarmInterval
				: min;
		min = min > mWaterLevelAlarmInterval ? mWaterLevelAlarmInterval : min;
		Log.d(TAG, "getMinAlarmInterval" + min);
		return min;

	}

	static BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(MESSAGE_SENT_ACTION)) {
				Log.d(TAG, MESSAGE_SENT_ACTION + getResultCode());
				switch (getResultCode()) {

				case Activity.RESULT_OK:
					Log.d(TAG, "send message success");
					setAlarmTime();
					break;

				default:
					Log.d(TAG, MESSAGE_SENT_ACTION + getResultCode());
					break;
				}
			}

		}
	};

	public boolean isNeedAlarm() {
		long oldtime = getAlarmTime();
		if (oldtime == 0) {
			return true;
		}
		long timeStamp = System.currentTimeMillis();
		long Interval = timeStamp - oldtime;
		Log.d(TAG, "isNeedAlarm" + Interval);
		if (Interval > getMinAlarmInterval() * 60 * 1000) {
			return true;
		}
		return false;
	}

	public String alarmPhInfo(float value) {
		if (value < mPhMin || value > mPhMax) {
			return getErrInfo(R.string.ph_value, value, mPhMin, mPhMax);
		}
		return null;

	}

	public String alarmTemperatureInfo(float value) {
		if (value < mTemperatureMin || value > mTemperatureMax) {
			return getErrInfo(R.string.temperature_value, value,
					mTemperatureMin, mTemperatureMax);
		}
		return null;

	}

	public String alarmOxygenInfo(float value) {
		if (value < mOxygenMin || value > mOxygenMax) {
			return getErrInfo(R.string.oxygen_value, value, mOxygenMin,
					mOxygenMax);
		}
		return null;
	}

	public String alarmConductivityInfo(float value) {
		if (value < mConductivityMin || value > mConductivityMax) {
			return getErrInfo(R.string.conductivity_value, value,
					mConductivityMin, mConductivityMax);
		}
		return null;

	}

	public String alarmWaterLevelInfo(float value) {
		if (value < mWaterLevelMin || value > mWaterLevelMax) {
			return getErrInfo(R.string.waterLevel_value, value, mWaterLevelMin,
					mWaterLevelMax);
		}
		return null;

	}

	private String getErrInfo(int id1, float value1, float value2, float value3) {
		String sInfoFormat = mContext.getResources().getString(
				R.string.err_info);
		String category = mContext.getResources().getString(id1);
		String sFinalInfo = String.format(sInfoFormat, category, value1,
				value2, value3);
		return sFinalInfo;
	}

	@Override
	public void update(Observable observable, Object data) {
		if (observable instanceof BTManager) {
			final DataStruct receivedData = (DataStruct) data;
			// byte[] resposeData = DataInterpreter.pack(receivedData);

			// for (int i = 0; i < receivedData.length; i++) {
			// Log.d(TAG, "data:" + Integer.toHexString(resposeData[i] & 0xFF));
			// }
			if (receivedData.prefix == 0x5a && receivedData.id == 0x01) {
				switch (receivedData.code) {
				case BTCommand.CODE_SET_HP_VALUE:
					float[] mPhValue = DataInterpreter
							.unpackPhAlarmData(receivedData.data);
					mPhMin = mPhValue[0];
					mPhMax = mPhValue[1];
					notifyGetData(GET_PH_MIN_KEY, String.valueOf(mPhMin));
					notifyGetData(GET_PH_MAX_KEY, String.valueOf(mPhMax));
					break;
				case BTCommand.CODE_SET_TEMPERATURE_VALUE:
					float[] mTempValue = DataInterpreter
							.unpackTemperatureAlarmData(receivedData.data);
					mTemperatureMin = mTempValue[0];
					mTemperatureMax = mTempValue[1];
					notifyGetData(GET_TEMPERATURE_MIN_KEY,
							String.valueOf(mTemperatureMin));
					notifyGetData(GET_TEMPERATURE_MAX_KEY,
							String.valueOf(mTemperatureMax));
					break;
				case BTCommand.CODE_SET_OXYGEN_VALUE:
					float[] mOxygenValue = DataInterpreter
							.unpackOxygenAlarmData(receivedData.data);
					mOxygenMin = mOxygenValue[0];
					mOxygenMax = mOxygenValue[1];
					notifyGetData(GET_OXYGEN_MIN_KEY,
							String.valueOf(mOxygenMin));
					notifyGetData(GET_OXYGEN_MAX_KEY,
							String.valueOf(mOxygenMax));
					break;
				case BTCommand.CODE_SET_CONDUCTIVITY_VALUE:
					float[] mConductivityValue = DataInterpreter
							.unpackConductivityAlarmData(receivedData.data);
					mConductivityMin = mConductivityValue[0];
					mConductivityMax = mConductivityValue[1];
					notifyGetData(GET_CONDUCTIVITY_MIN_KEY,
							String.valueOf(mConductivityMin));
					notifyGetData(GET_CONDUCTIVITY_MAX_KEY,
							String.valueOf(mConductivityMax));
					break;
				case BTCommand.CODE_SET_WATER_LEVEL_VALUE:
					float[] mWaterLevelValue = DataInterpreter
							.unpackWaterLevelAlarmData(receivedData.data);
					mWaterLevelMin = mWaterLevelValue[0];
					mWaterLevelMax = mWaterLevelValue[1];
					notifyGetData(GET_WATER_LEVEL_MIN_KEY,
							String.valueOf(mWaterLevelMin));
					notifyGetData(GET_WATER_LEVEL_MAX_KEY,
							String.valueOf(mWaterLevelMax));
					break;

				default:
					break;
				}
			}
		}

	}

}
